<?php

namespace App\Http\Middleware;
use App\Dao\UsersDao;
use App\Services\Code;
use Closure;
use Illuminate\Http\Request;
class UserMiddleware
{


    /**
     * Handle an incoming request.
     *
     * @param  Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(request()->header('uid') == ''){
            echo json_encode(Code::message('用户未登陆',201101),JSON_UNESCAPED_UNICODE);exit;//没有传用户的Header
        }
//        if(request()->header('tokenp') == ''){
//            echo json_encode(Code::message('用户未登陆',201101),JSON_UNESCAPED_UNICODE);exit;//没有传用户的Header
//        }
        $request->user_id = request()->header('uid');
        $query = UsersDao::get_user_builder(['user_id'=>$request->user_id]);
        if(!$query->exists()){
            echo json_encode(Code::message('用户不存在',201101),JSON_UNESCAPED_UNICODE);exit;//没有传用户的Header
        }
        $userInfo = $query->first()->toArray();
//        if(md5($userInfo['user_phone']) != request()->header('tokenp')){
//            echo json_encode(Code::message('用户token不正确',201101),JSON_UNESCAPED_UNICODE);exit;//没有传用户的Header
//        }
        return $next($request);
    }

}
